package com.my.commonlib.util;

import android.text.TextUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class JsonUtility {

    private final static ObjectMapper mMapper = new ObjectMapper();

    static {
        //1.空值转换-异常情况：
        mMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        //1-1.空值转换-异常情况：
        mMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        //2. 转义字符-异常情况
        mMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;
        //3/配置为false表示mapper在遇到mapper对象中存在json对象中没有的数据变量时不报错，可以进行反序列化
        mMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    }

    /**
     *  JsonObject获取 key 对应Value
     * @param object
     * @param key
     * @return
     */
    public static String optString(JSONObject object, String key) {
        try {
            String text = object.optString(key);
            return TextUtils.isEmpty(text) ? null : text;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String optString(String jsonStr, String key) {
        if(!TextUtils.isEmpty(jsonStr)){
            try{
                return optString(strConvertJO(jsonStr),key);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * JsonObject 转换成 实体
     * @param jsonStr
     * @param c
     * @param <T>
     * @return
     */
    public static <T> T convertJS2Obj(String jsonStr, Class<T> c) {
        if (!TextUtils.isEmpty(jsonStr)) {
            try {
                return mMapper.readValue(jsonStr,c);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            return c.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static <T> T convertJS2Obj(byte[] bytes, Class<T> c) {
        if (bytes.length < 0) {
            try {
                return mMapper.readValue(bytes,c);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            return c.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * JsonStr 返回List
     * @param jsonStr
     * @param c
     * @param <T>
     * @return
     */
    public static <T>List<T> convertJS2List(String jsonStr,final Class<T> c){
        try{
            JavaType javaType = getCollectionType(List.class, c);
            return mMapper.readValue(jsonStr, javaType);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return mMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public static String obj2JOStr(Object obj) {
        try {
            return mMapper.writeValueAsString(obj);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject strConvertJO(String str) {
        try {
            return new JSONObject(str);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
